﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfAGVSystem.Common
{
    /// <summary>
    /// 数学类
    /// </summary>
    public static class PineMath
    {
        /// <summary>
        /// 线程等待最小数值
        /// </summary>
        public static readonly int MinTime = 100;
        /// <summary>
        /// 线程等待中间数值
        /// </summary>
        public static readonly int MidTime = 500;
        /// <summary>
        /// 线程等待最大数值
        /// </summary>
        public static readonly int MaxTime = 1000;
        public static readonly int MaxMaxTime = 5000;
        /// <summary>
        /// 堆垛AGV举升最小高度
        /// </summary>
        public static readonly float MinHeight = 0.09f;
        /// <summary>
        /// 堆垛AGV举升最高高度
        /// </summary>
        public static readonly float MaxHeight  = 3f;
        /// <summary>
        /// 设置高度与AGV实际高度相差数值
        /// </summary>
        public static readonly float DifferenceHeight = 0.02f;
        /// <summary>
        /// AGV避让在避让点停留时间
        /// </summary>
        public static readonly int AvoidTime = 15;
        /// <summary>
        /// log日志内存缓存个数
        /// </summary>
        public static readonly int LogsNumber = 200;
        public static readonly int MinLogsNumber = 100;
        /// <summary>
        /// log日志 保存在文件夹中只保存三十天
        /// </summary>
        public static readonly int SaveInfo = 60;
        public static void ExGetUshort(this float val, ref ushort upper, ref ushort lower)
        {
            byte[] bytes = BitConverter.GetBytes(val);
            upper = (ushort)BitConverter.ToInt16(bytes, 0);
            lower = (ushort)BitConverter.ToInt16(bytes, 2);
        }

        public static string ConvertArr2Str<T>(T[] arr)
        {
            var reval = new StringBuilder();
            if(arr == null) { return reval.ToString(); }
            foreach (var item in arr?? Enumerable.Empty<T>())
            {
                reval.Append($"{item} , ");
            }
            return reval.ToString();
        }

        public static int GrowthItem(List<int> lists)
        {
            if (lists == null) return 1;
            int index = 0;
            foreach(int list in lists)
            {
                if (list > index) index = list;
            }
            index++;
            return index;
        }


        public static float GetDifference(float f1,float f2)
        {
            f1 = f1 - f2;
            return Math.Abs(f1);
        }
        public static double GetDifference(double d1, double d2)
        {
            d1 = d1 - d2;
            return Math.Abs(d1);
        }

        /// <summary>
        /// 判断当天是否是月底
        /// </summary>
        /// <returns></returns>
        public static bool IsMonthEnd(int days)
        {
            if (days <= 0) return false;
            DateTime dateTime1 = DateTime.Now;
            DateTime dateTime2 = dateTime1.AddDays(days);
            return dateTime1.Month == dateTime2.Month ? false : true;
        }

        public static int IsInt(string str)
        {
            int.TryParse(str, out int result);
            return result;
        }
    }
}
